package com.duguo.dynamicmvc.demo.accounts;

import com.duguo.dynamicmvc.demo.accounts.domain.User;
import com.duguo.dynamicmvc.demo.accounts.domain.UserImpl;
import com.duguo.dynamicmvc.demo.accounts.model.ChangePasswordFormModel;
import com.duguo.dynamicmvc.demo.accounts.model.LoggedinUserResolver;
import com.duguo.dynamicmvc.demo.accounts.model.LoginFormModel;
import com.duguo.dynamicmvc.demo.accounts.model.ReturnUrlModel;
import com.duguo.dynamicmvc.model.RequestModel;
import com.duguo.dynamicmvc.model.statusmessage.StatusMessageImpl;
import com.duguo.dynamicmvc.model.statusmessage.StatusMessageModel;
import com.duguo.dynamicmvc.model.validator.exception.ValidationException;
import com.duguo.dynamicmvc.view.View;
import com.duguo.dynamicmvc.view.redirect.HttpRedirectView;
import com.duguo.dynamicmvc.view.redirect.RedirectToRefererView;
import com.duguo.dynamicmvc.view.redirect.RedirectView;

public class AccountsController{
	
	public void login(ReturnUrlModel model) throws ValidationException{
    	if(model.getReturnUrl()==null){
    		String returnUrl=RedirectToRefererView.getReferer(model.getRequest());
    		if(returnUrl==null || returnUrl.matches(".*/login") || returnUrl.matches(".*/do[A-Z].*")){
    			returnUrl="/";
    		}
    		model.setReturnUrl(returnUrl);
    	}
    }

    public View doLogin(LoginFormModel model, View view) throws ValidationException{
        if(model.getUserName().equals(model.getPassword())){
        	User user=new UserImpl(System.currentTimeMillis(),model.getUserName(),model.getPassword());
        	model.getRequest().getSession().setAttribute(LoggedinUserResolver.LOGGEDIN_USER, user);
        }else{
        	model.addStatusMessage(new StatusMessageImpl(2,"Login Failed","Incorrect username or password!"));
        	throw new ValidationException();
        }
        if(model.getReturnUrl().charAt(0)=='/')
        	return new RedirectView(model.getReturnUrl());
        else
        	return new HttpRedirectView(model.getReturnUrl());
    }

    public void doLogout(RequestModel model) throws ValidationException{
        model.getRequest().getSession().removeAttribute(LoggedinUserResolver.LOGGEDIN_USER);
    	((StatusMessageModel)model).addStatusMessage(new StatusMessageImpl("Logout successfully!"));
    }

    public void doChangePassword(ChangePasswordFormModel model) throws ValidationException{
    	if(!model.getRetypePassword().equals(model.getNewPassword())){
        	((StatusMessageModel)model).addStatusMessage(new StatusMessageImpl(2,"Error","New password doesn't match with retyped password!"));
    	}else if(model.getLoggedinUser().getPassword().equals(model.getOldPassword())){
    		model.getLoggedinUser().setPassword(model.getNewPassword());
        	((StatusMessageModel)model).addStatusMessage(new StatusMessageImpl("Change password successfully!"));
            return;
    	}else{
        	((StatusMessageModel)model).addStatusMessage(new StatusMessageImpl(2,"Error","Incorrect password!"));    		
    	}
    	throw new ValidationException();
    }
}
